using System;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Diagnostics;

namespace servermon_library
{
	public class DbHandler
	{
		// DBProviderFactory allows us to create a DB-independent program. For ease of distribution we use an access database for now (see App.config)
		private static ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["servermon"];
		private static DbProviderFactory factory = DbProviderFactories.GetFactory(conSettings.ProviderName);

		public static DbConnection Connection()
		{
			DbConnection connection = factory.CreateConnection();
			connection.ConnectionString = conSettings.ConnectionString;
			return connection;
		}

		public static DataTable ExecSQLReturnDT(String sqlString)
		{
			DbConnection connection = Connection();
			DataTable result = new DataTable();
			connection.Open();
			using (DbCommand command = connection.CreateCommand())
			{
				command.CommandType = CommandType.Text;
				command.CommandText = sqlString;
				using (DbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
				{
					result.Load(reader);
				}
			}
			return result;
		}

		public static Int32 ExecSQLNonQuery(String sqlString)
		{
			DbConnection connection = Connection();
			using (DbCommand command = connection.CreateCommand())
			{
				Int32 result;
				command.CommandType = CommandType.Text;
				command.CommandText = sqlString;
				connection.Open();
				result = command.ExecuteNonQuery();
				connection.Close();
				return result;
			}
		}
	}
}
